package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.bean.MonthlyMoneyBean;
import com.project.bean.OrderBean;
import com.project.bean.ShoppingCarBean;
import com.project.mapper.IOrderMapper;
import com.project.mapper.IOriderDetailsMapper;
import com.project.service.IOrderService;
import com.project.service.IShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/8/1 12:16
 */
@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper mapper;
    @Autowired
    private IShoppingCarService service;
    @Autowired
    private IOriderDetailsMapper oriderDetailsMapper;

    @Override
    public OrderBean findById(int id) {
        return mapper.findById(id);
    }

    @Override
    public PageInfo<OrderBean> findByItemAdmin(int pageNo, String status, LocalDate startDate, LocalDate endDate, Integer id) {
        PageHelper.startPage(pageNo, 3);
        PageInfo<OrderBean> pageInfo = PageInfo.of(mapper.findByItemAdmin(status, startDate, endDate, id));
        return pageInfo;
    }

    @Override
    public List<OrderBean> findByItemDeBoy() {
        return mapper.findByItemDeBoy();
    }

    @Override
    public void add(OrderBean orderBean, int id) {
        System.out.println(orderBean);
        List<ShoppingCarBean> list = service.findByUser(orderBean.getUserBean().getId());
        if (list.size()!=0){
            mapper.add(orderBean);
            oriderDetailsMapper.addAll(list, orderBean.getId());
            for (ShoppingCarBean shoppingCarBean : list) {
                service.del(shoppingCarBean.getId());
            }
            int money=oriderDetailsMapper.sumMoney(orderBean.getId());
            mapper.updateMoney(orderBean.getId(),money);
        }else {
            System.out.println("当前客户购物车为空，不能做添加订单处理业务");
        }
    }

    @Override
    public void send(int id) {
        mapper.update(id, "发货中");
    }

    @Override
    public void finish(int id) {
        mapper.update(id, "已完结");
    }

    @Override
    public List<MonthlyMoneyBean> findByDate(LocalDate date,LocalDate end) {
        return mapper.findByDate(date,end);
    }
}
